"""
Case Type   : Ustore_Datatype_Interval
Case Name   : interval year to month有效值测试，成功
Create At   : 2022/02/25
Owner       : lvlintao666
Description :
    1.interval year to month有效值测试
    2.环境清理
Expect      :
    1.成功
    2.成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant


class Interval(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

        text = "-----检查参数，修改配置:enable_default_ustore_table=on-----"
        self.logger.info(text)
        self.config_ustore = 'enable_default_ustore_table'
        self.ustore_cmd = f'show {self.config_ustore};'
        check_res = self.sh_primy.execut_db_sql(self.ustore_cmd)
        self.logger.info(check_res)
        self.default_ustore = check_res.splitlines()[-2].strip()
        if 'on' != self.default_ustore:
            result = self.sh_primy.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'{self.config_ustore}=on')
            self.assertTrue(result, "执行失败" + text)
        ustore_msg = self.sh_primy.execut_db_sql(self.ustore_cmd)
        self.logger.info(ustore_msg)
        self.common.equal_sql_mdg(ustore_msg, self.config_ustore, 'on',
                                  '(1 row)', flag='1')

    def test_interval(self):
        text = "-----step1:interval year to month有效值测试，expect:成功-----"
        self.logger.info(text)
        sql_cmd = f'''select interval '9999-11' year(4) to month ;
                      select interval '-9999-11' year(4) to month ;'''
        self.logger.info(sql_cmd)
        sql_msg = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn("9999 years 11 mons",
                      sql_msg.splitlines()[2], "执行失败" + text)
        self.assertIn("-9999 years -11 mons",
                      sql_msg.splitlines()[-2], "执行失败" + text)

    def tearDown(self):
        text = "-----step2:环境清理，expect:成功-----"
        self.logger.info(text)
        text = "-----检查参数，恢复默认配置:enable_default_ustore_table-----"
        self.logger.info(text)
        check_res = self.sh_primy.execut_db_sql(self.ustore_cmd)
        self.logger.info(check_res)
        current = check_res.splitlines()[-2].strip()
        if self.default_ustore != current:
            result = self.sh_primy.execute_gsguc('reload',
                                self.constant.GSGUC_SUCCESS_MSG,
                                f'{self.config_ustore}={self.default_ustore}')
            self.assertTrue(result, "执行失败" + text)

        show_msg = self.sh_primy.execut_db_sql(self.ustore_cmd)
        self.logger.info(show_msg)
        self.common.equal_sql_mdg(show_msg, self.config_ustore,
                                  self.default_ustore, '(1 row)', flag='1')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")